package com.example.myapplication.tack_13;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.myapplication.R;
import com.example.myapplication.tack_13.data.CardData;
import com.example.myapplication.utils.Common;
import com.example.myapplication.utils.OkHttpTool;
import com.example.myapplication.utils.StateData;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class SetCardActivity extends AppCompatActivity {
    private ImageView exit;
    private EditText address ;
    private EditText birthday;
    private EditText cardId;
    private EditText name ;
    private RadioGroup radio_group ;
    private RadioButton radioButton1 ;
    private RadioButton radiofButton2 ;
    private EditText tel ;
    private TextView save;
    private TextView department;
    private JSONObject jsonObject;
    private  String jsonSex;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tack_13_activity_set_card);
        init();
        exit_();
        Intent intent = getIntent();
        String id = intent.getStringExtra(Common.COMMON);
        save_(id);
        getCard(id);
        department_();


    }

    private void department_() {
        department.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent1 = new Intent(SetCardActivity.this,DepartmentActivity.class);
                SetCardActivity.this.startActivity(intent1);
            }
        });
    }

    private void save_(String id) {
        save.setOnClickListener(new View.OnClickListener() {
            private  Date date = null;
            private String dateStr;
            @Override
            public void onClick(View v) {

                String address1 = address.getText().toString();
                String birthday1 = birthday.getText().toString();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                try {
                    date = sdf.parse(birthday1);
                    dateStr = sdf.format(date);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                radio_group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(RadioGroup group, int checkedId) {
                        if(R.id.radio1 == group.getCheckedRadioButtonId()) {
                            jsonSex = "1";
                        }else if(group.getCheckedRadioButtonId() ==  R.id.radio2){
                            jsonSex = "0";
                        }
                    }
                });

                String cardId1 = cardId.getText().toString();
                String name1 = name.getText().toString();
                String tel1 = tel.getText().toString();
                try {

                    jsonObject.put("id",id);
                    jsonObject.put("address",address1);
                    jsonObject.put("birthday",dateStr);
                    jsonObject.put("cardId",cardId1);
                    jsonObject.put("name",name1);
                    jsonObject.put("sex",jsonSex);
                    jsonObject.put("tel",tel1);
                    if(("".equals(jsonSex) || TextUtils.isEmpty(jsonSex) ) && ("".equals(address1) || TextUtils.isEmpty(address1))
                            && ("".equals(birthday1) || TextUtils.isEmpty(birthday1)) && ("".equals(cardId1) || TextUtils.isEmpty(cardId1))
                            && ("".equals(name1) || TextUtils.isEmpty(name1)) && ("".equals(tel1) || TextUtils.isEmpty(tel1))){
                        Toast.makeText(SetCardActivity.this,"请输入信息",Toast.LENGTH_SHORT).show();
                    }else {
                        String json = jsonObject.toString();
                        setCard(json);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });
    }

    private void setCard(String json) {
        String url = "prod-api/api/hospital/patient";
        OkHttpTool.doNetRequest(url, OkHttpTool.PUT, StateData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                StateData stateData = (StateData)obj;
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if(stateData.getCode() == 200){
                            Toast.makeText(SetCardActivity.this,stateData.getMsg(),Toast.LENGTH_SHORT).show();
                            SetCardActivity.this.finish();
                        }else{
                            Toast.makeText(SetCardActivity.this,stateData.getMsg(),Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }

            @Override
            public void onFailure(String msg) {

            }
        },json);
    }

    private void getCard(String id) {
        int integer = (int)Integer.parseInt(id);
        integer -= 10086;
        String url = "prod-api/api/hospital/patient/list?pageNum="+integer+"&pageSize=1";
        OkHttpTool.doNetRequest(url, OkHttpTool.GET, CardData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                CardData cardData = (CardData)obj;

                if(cardData.getCode() == 200){
                    runOnUiThread(new Runnable() {
                        List<CardData.RowsDTO> list = cardData.getRows();
                        @Override
                        public void run() {
                            address.setText(list.get(0).getAddress());
                            birthday.setText(String.valueOf(list.get(0).getBirthday()));
                            cardId.setText(String.valueOf(list.get(0).getCardId()));
                            name.setText(String.valueOf(list.get(0).getName()));
                            String sex = String.valueOf(list.get(0).getSex());
                            if("1".equals(sex)){
                                radioButton1.setChecked(true);
                            }else if("0".equals(sex)){
                                radiofButton2.setChecked(true);
                            }
                            tel.setText(String.valueOf(list.get(0).getAddress()));
                        }
                    });
                }
            }

            @Override
            public void onFailure(String msg) {

            }
        });
    }

    private void exit_() {
        exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SetCardActivity.this.finish();
            }
        });
    }

    private void init() {
        address = findViewById(R.id.address);
        birthday = findViewById(R.id.birthday);
        cardId = findViewById(R.id.cardId);
        name = findViewById(R.id.name);
        radio_group = findViewById(R.id.radio_group);
        radioButton1 = findViewById(R.id.radio1);
        radiofButton2 = findViewById(R.id.radio2);
        department = findViewById(R.id.department);
        jsonObject = new JSONObject();
        tel = findViewById(R.id.tel);
        exit = findViewById(R.id.exit);
        save = findViewById(R.id.save);
    }
}